/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Views.GestionRestaurant;

import Controller.Controller;
import Model.Restaurateur;
import Views.GestionRestaurateur.*;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author ibrahim
 */
public class RestaurantPanel extends JPanel {
    
    
    public RestaurantPanel(Controller controller){
     
      
    Dimension dim =getPreferredSize();
    dim.width=250;
    setPreferredSize(dim);
    
    
    this.NameLabel = new JLabel("Nom: ");
    this.StreetNumLabel = new JLabel("Numero de la rue: ");
    this.CityLabel = new JLabel("Ville: ");
    this.PostalCodeLabel = new JLabel("Code Postal: ");
    this.restaurateurLabel = new JLabel("Restaurateur: ");
    this.nameTextFeild = new JTextField(10);
    this.cityTextFeild = new JTextField(10);
    this.streetnumberTextFeild = new JTextField(10);
    this.postalcodeTextFeild = new JTextField(10);
    this.listRestaurateurs = new JComboBox();
    this.submitButton = new JButton ("Ok");
    this.saveButton = new JButton ("Save");
    Dimension btnSize = saveButton.getPreferredSize();
    this.submitButton.setPreferredSize(btnSize);
    this.saveButton.setPreferredSize(btnSize);
      
      
    //set up Restaurateur ComboBox Model
    DefaultComboBoxModel restComboModel = new DefaultComboBoxModel();
    restComboModel.addElement("Non Assigne");
   for(Restaurateur rest:controller.getRestauraterurs()){
  restComboModel.addElement(rest.getFname()+" "+ rest.getLname());
        System.out.println(rest.getLname()+ " " +rest.getFname());
    }  
    
    //restComboModel.addElement(name);
    this.listRestaurateurs.setModel(restComboModel);
    
    
    
    
    //Set up Memonics
    this.submitButton.setMnemonic(KeyEvent.VK_O);
    
    
    
    
    this.submitButton.addActionListener(new ActionListener() {

       
        public void actionPerformed(ActionEvent e) {
            String name = nameTextFeild.getText();
            int streetNum = Integer.parseInt(streetnumberTextFeild.getText());
            String city = cityTextFeild.getText();
            String postalCode = postalcodeTextFeild.getText();
            String restaurateur = (String)listRestaurateurs.getSelectedItem();
           AddRestaurantEvent arEv = new AddRestaurantEvent(this,name,streetNum,city,postalCode,restaurateur);
           if(arListener!=null){
                arListener.AddRestaurantEventOccurred(arEv);
                
            }
        }
    });
    
    this.saveButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(sdbListener!=null){
                sdbListener.savetodatabaseEventOccured();
            }
        }
    });
    
    
    Border innerBorder = BorderFactory.createTitledBorder("Gestion Restaurant");
    Border outerBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
    setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));
    
    setLayout(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();
    //addRestaurateursComboBox();
    //First Row
    
    gc.gridy=0;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.fill = GridBagConstraints.NONE;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.NameLabel,gc);
    gc.gridx=1;
    gc.gridy=0;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.nameTextFeild,gc);
    //Second Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.StreetNumLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.streetnumberTextFeild,gc);       
    //Third Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.CityLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.cityTextFeild,gc);
    //Fourth Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.PostalCodeLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.postalcodeTextFeild,gc);
    //Fifth Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=0.2;
    gc.gridx=0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets (0,0,0,5);
    add(this.restaurateurLabel,gc);
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.listRestaurateurs,gc);
    //Eighth Row
    gc.gridy++;
    gc.weightx=1;
    gc.weighty=1;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.submitButton,gc);
    //Last Row
    gc.weightx=1;
    gc.weighty=1;
    gc.gridy++;
    gc.gridx=1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets (0,0,0,0);
    add(this.saveButton,gc);
    
    
    }
    public void setAddRestaurantListener(AddRestaurantListener arListener){
        this.arListener = arListener;
    }
    public void setSaveToDatabaseListener(SaveToDatabaseListener sdbListener){
        this.sdbListener = sdbListener;
    }
   
   
    

    private JLabel NameLabel;
    private JLabel StreetNumLabel;
    private JLabel CityLabel;
    private JLabel PostalCodeLabel;
    private JLabel restaurateurLabel;
    private JTextField nameTextFeild;
    private JTextField cityTextFeild;
    private JTextField streetnumberTextFeild;
    private JTextField postalcodeTextFeild;
    private JButton submitButton;
    private JButton saveButton;
    private JComboBox listRestaurateurs;
    private AddRestaurantListener arListener;
    private SaveToDatabaseListener sdbListener;
    
   
    
   

    
}
